//
//  PageTitleView.swift
//  CBLearnDY
//
//  Created by ncimall on 2017/11/16.
//  Copyright © 2017年 ncimall. All rights reserved.
//

import UIKit

//  MARK:- 代理定义
protocol PageTitleViewDelegate: class {
    func pageTitleViewClicked(titleView: PageTitleView, selectIndex index: Int)
}

private let kScrollLineH : CGFloat = 2

class PageTitleView: UIView {
    private var pageTitles : [String]
    fileprivate var currentIndex : Int = 0
    
    //  定义代理对象
    weak var delegate: PageTitleViewDelegate?
    
    private lazy var scrollView : UIScrollView = {
        let scrollView = UIScrollView.init()
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        
        return scrollView
    }()
    
    private lazy var titleLabels : [UILabel] = [UILabel]()
    
    private lazy var scrollLine : UIView = {
        let scrollLine = UIView.init()
        scrollLine.backgroundColor = UIColor.orange
        
        return scrollLine
    }()
    
    //  自定义构造函数
    init(frame: CGRect, pageTitles : [String]) {
        self.pageTitles = pageTitles
        
        super.init(frame: frame)
        
        //设置UI界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//  MARK: - 设置UI
extension PageTitleView {
    fileprivate func setupUI() {
        //  添加UIScrollView
        addSubview(scrollView)
        scrollView.frame = bounds
        
        //  设置title
        setupTitleLabel()
        setupBottomLineAndScollLine()
    }
    
    fileprivate func setupTitleLabel() {
        
        let labelW : CGFloat = frame.width / CGFloat(pageTitles.count)
        let labelH : CGFloat = frame.height - kScrollLineH
        let labelY : CGFloat = 0
        
        for (index, title) in pageTitles.enumerated() {
            // 创建label
            let label = UILabel.init()
            label.text = title
            label.tag = index
            label.font = UIFont.systemFont(ofSize: 16.0)
            label.textColor = UIColor.darkGray
            label.textAlignment = .center
            
            //  设置frame
            let labelX : CGFloat = CGFloat(index) * labelW
            label.frame = CGRect.init(x: labelX, y: labelY, width: labelW, height: labelH)
            scrollView.addSubview(label)
            
            titleLabels.append(label)
            
            //  添加点击手势
            label.isUserInteractionEnabled = true
            let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(titleLabelClicked(tapGes:)))
            label.addGestureRecognizer(tapGes)
        }
    }
    
    fileprivate func setupBottomLineAndScollLine() {
        //  添加底线
        let bottomLine = UIView.init()
        let bottomLineH : CGFloat = 0.5
        bottomLine.backgroundColor = UIColor.lightGray
        bottomLine.frame = CGRect.init(x: 0, y: frame.height - bottomLineH, width: frame.width, height: bottomLineH)
        addSubview(bottomLine)
        
        //  添加scrollLine
        guard let firtLabel = titleLabels.first else {
            return
        }
        firtLabel.textColor = UIColor.orange
        
        scrollView.addSubview(scrollLine)
        scrollLine.frame = CGRect.init(x: firtLabel.frame.origin.x, y: frame.height - kScrollLineH, width: firtLabel.frame.width, height: kScrollLineH)
    }
}

//  MARK:- 手势点击
extension PageTitleView {
    @objc fileprivate func titleLabelClicked(tapGes: UITapGestureRecognizer) {
        guard let currentLabel = tapGes.view as? UILabel else {
            return
        }
        
        let oldLabel = titleLabels[currentIndex]
        
        if oldLabel == currentLabel {
            return
        }
        
        currentLabel.textColor = UIColor.orange
        oldLabel.textColor = UIColor.darkGray
        
        currentIndex = currentLabel.tag
        
        let scrollLinePos = CGFloat(currentIndex) * scrollLine.frame.width
        UIView.animate(withDuration: 0.15) {
            self.scrollLine.frame.origin.x = scrollLinePos
        }
        
        delegate?.pageTitleViewClicked(titleView: self, selectIndex: currentIndex)
    }
}

//  MARK:-  暴露的方法
extension PageTitleView {
    func changeCurrentSelectState(selectIndex: Int) {
        
        if currentIndex == selectIndex {
            return
        }
        
        let currentLabel = titleLabels[selectIndex]
        let oldLabel = titleLabels[currentIndex]
        
        currentLabel.textColor = UIColor.orange
        oldLabel.textColor = UIColor.darkGray
        currentIndex = selectIndex
        
        let scrollLinePos = CGFloat(currentIndex) * scrollLine.frame.width
        UIView.animate(withDuration: 0.15) {
            self.scrollLine.frame.origin.x = scrollLinePos
        }
    }
}
